<?php

namespace App\Admin\Controllers;

use App\Models\Notice;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;

class NoticeController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('通知公告发布');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {


            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {


            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Notice::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->disableExport();
            $grid->disableFilter();
            $grid->id('ID')->sortable();
            $grid->type('类型')->display(function ($val){
                return config('options.notice_type')[$val];
            });
            $grid->module('模块')->display(function ($val){
                return config('options.notice_module')[$val];
            });
            $grid->content('内容');
            $states = [
                'on'  => ['value' => 1, 'text' => '显示', 'color' => 'primary'],
                'off' => ['value' => 0, 'text' => '取消', 'color' => 'default'],
            ];
            $grid->display('是否显示')->switch($states);
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Notice::class, function (Form $form) {
            $form->radio('type','类型')->options(config('options.notice_type'));
            $form->radio('module','模块')->options(config('options.notice_module'));
            $form->text('title');
            $form->textarea('content');
            $states = [
                'on'  => ['value' => 1, 'text' => '显示', 'color' => 'primary'],
                'off' => ['value' => 0, 'text' => '取消', 'color' => 'default'],
            ];//
            $form->switch('display','显示')->states($states);
            $form->multipleSelect('release','信息发布')->options(config('options.release_platform'));
        });
    }
}
